<?php

const PRA_LOGIN_PAGE = true;
global $praLoginError;

require_once 'includes/common.inc.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && !pra_auth($_POST)) {
	$praLoginError = 'Invalid username or password!';
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>phpRedisAdmin - User authentication</title>
	<script type="text/javascript" src="js/jquery.js"></script>
	<script type="text/javascript" src="js/md5.min.js"></script>
</head>
<body>
<form id="login_form" method="post">
	<div>
		<label>Username:<br/><input type="text" id="username_value" name="username" value="" /></label>
	</div>
	<div>
		<label>Password:<br/><input type="password" id="password_input" value="" /></label>
	</div>
	<div>
		<input type="hidden" id="password_value" name="password" value="" />
		<button>Login</button>
	</div>
	<span id="login_tip"><?php print $praLoginError; ?></span>
</form>
<script type="text/javascript">
	(function($) {

		function _trim(x) {
			return x.replace(/^\s+|\s+$/gm,'');
		}

		function verify() {
			var username = _trim($('#username_value').val());
			var password = _trim($('#password_input').val());
			if (username.length <= 0 || password.length <= 0) {
				$('#login_tip').text('username or password cant be empty!');
				return false;
			}
			else {
				$('#login_tip').text('');
			}
			return true;
		}

		function passwordHash() {
			var password = _trim($('#password_input').val());
			var hashed = md5(password);
			$('#password_value').val(hashed);
		}

		$('#login_form').submit(function() {
			if (verify()) {
				passwordHash();
			}
			else {
				return false;
			}

		});

	}) (jQuery);
</script>
</body>
</html>